﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
/// <summary>
/// Summary description for Product
/// </summary>
public class Product
{

    DataClassesDataContext dtc;

    public Product()
    {
        //
        // TODO: Add constructor logic here
        //
        this.dtc = new DataClassesDataContext();
    }
    #region movie
    public List<tblMovie> GetMovie(DateTime date)
    {
        var x = from i in this.dtc.tblMovies
                where i.ReleasedDay <= date && i.CloseDay >= date && i.StatusM == 0
                orderby i.MovieID descending
                select i;
        return x.ToList();
    }
    public List<tblMovie> GetMovieY(DateTime date)
    {
        var x = from i in this.dtc.tblMovies
                where i.ReleasedDay >= date
                orderby i.MovieID descending
                select i;
        return x.ToList();
    }
    public List<tblMovie> GetMovieById(string id)
    {
        var x = from i in this.dtc.tblMovies
                where i.MovieID.Equals(id)
                orderby i.MovieID descending
                select i;
        return x.ToList();
    }
    public tblMovie GetMovieById2(string id)
    {
        tblMovie x = (from i in this.dtc.tblMovies
                      where i.MovieID.Equals(id)
                      orderby i.MovieID descending
                      select i).First();
        return x;
    }
    public string GetMovieNameById(string id)
    {
        tblMovie x = (from i in this.dtc.tblMovies
                      where i.MovieID.Equals(id)
                      orderby i.MovieID descending
                      select i).First();
        return x.MovieName;
    }
    public List<tblMovie> GetMovieByName(string Name)
    {
        var x = from i in this.dtc.tblMovies
                      where i.MovieName.Contains(Name)
                      select i;
        return x.ToList();
    }
    public string getID()
    {
        tblMovie d = (from t in this.dtc.tblMovies
                      where t.Stt == this.dtc.tblMovies.Max(t1 => t1.Stt)
                      select t).First();
        int id = d.Stt + 1;
        return "MV0" + id;

    }
    public List<tblMovie> GetMovieAll(DateTime date)
    {
        var x = from i in this.dtc.tblMovies
                where i.CloseDay >= date
                orderby i.MovieID ascending
                select i;
        return x.ToList();
    }
    public string DelMovie(string movieid)
    {
        var x = from i in this.dtc.tblMovies
                where i.MovieID.Equals(movieid)
                select i;
        this.dtc.tblMovies.DeleteOnSubmit(x.First());
        this.dtc.SubmitChanges();
        return "Delete movies";
    }
    public bool UpdateMovie(string movieid,string name,string director,string actors,string ca,string country,string avata,string about,DateTime start,DateTime end,int status,string notes,int cl)
    {
        try
        {
            tblMovie mv1 = (from i in dtc.tblMovies where i.MovieID == movieid select i).Single();
            mv1.MovieName = name;
            mv1.FilmDirector = director;
            mv1.Actors = actors;
            mv1.Category = ca;
            mv1.country = country;
            if(cl==1)
            {
              mv1.Avata = avata;
            }
            mv1.AboutUsFile = about;
            mv1.ReleasedDay = start.Date;
            mv1.CloseDay = end.Date;
            mv1.StatusM = status;
            mv1.Notes = notes;
            dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;
        
    }
    public bool AddMovie(string movieid, string name, string director, string actors, string ca, string country, string avata, string about, DateTime start, DateTime end, int status, string notes)
    {
        try
        {
            tblMovie mv1 = new tblMovie();
            mv1.MovieID = movieid;
            mv1.MovieName = name;
            mv1.FilmDirector = director;
            mv1.Actors = actors;
            mv1.Category = ca;
            mv1.country = country;
            mv1.Avata = avata;
            mv1.AboutUsFile = about;
            mv1.ReleasedDay = start.Date;
            mv1.CloseDay = end.Date;
            mv1.StatusM = status;
            mv1.Notes = notes;
            dtc.tblMovies.InsertOnSubmit(mv1);
            dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    #endregion
    #region Product
    public bool AddProduct(string prid,string shopid,string catid,string catalogID,string proName,float price ,string name)
    {
        try
        {
            tblProduct pr = new tblProduct();
            pr.ProductID = prid;
            pr.ShopDID = shopid;
            pr.CategoriesID = catid;
            pr.CatalogID = catalogID;
            pr.ProductName = proName;
            pr.Price = price;
            pr.PhotoName = name;
            this.dtc.tblProducts.InsertOnSubmit(pr);
            this.dtc.SubmitChanges();
        }
        catch(Exception)
        {
            return false;
        }
        return true;

    }
    public bool UpdateProduct(string prid, string shopid, string catid, string catalogID, string proName, float price, string name,int cl)
    {
        try
        {
            tblProduct pr = (from i in this.dtc.tblProducts where i.ProductID == prid select i).Single();
            pr.ShopDID = shopid;
            pr.CategoriesID = catid;
            pr.CatalogID = catalogID;
            pr.ProductName = proName;
            pr.Price = price;
            if(cl==1)
            {
            pr.PhotoName = name;
            }
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    public string getProductID()
    {
        tblProduct d = (from t in this.dtc.tblProducts
                      where t.Stt == this.dtc.tblProducts.Max(t1 => t1.Stt)
                      select t).First();
        int id = d.Stt + 1;
        return "PR0" + id;

    }
    public List<tblProduct> GetProductByName(string Name)
    {
        var x = from i in this.dtc.tblProducts
                where i.ProductName.Contains(Name)
                select i;
        return x.ToList();
    }
    public List<tblProduct> GetProduct()
    {
        var x = from i in this.dtc.tblProducts
                orderby i.ProductID ascending
                select i;
        return x.ToList();
    }
    public bool DelProduct(string prid)
    {
        try
        {
            var x = from i in this.dtc.tblProducts
                    where i.ProductID.Equals(prid)
                    select i;
            this.dtc.tblProducts.DeleteOnSubmit(x.First());
            this.dtc.SubmitChanges();
        }
        catch(Exception)
        {
            return false;
        }
        return true;
    }
    public tblProduct GetProductById(string id)
    {
           tblProduct x = (from i in this.dtc.tblProducts
                           where i.ProductID == id
                           select i).First();
        return x;
    }
    public bool UpdateProduct2(string prid,string proName, float price)
    {
        try
        {
            tblProduct pr = (from i in this.dtc.tblProducts where i.ProductID == prid select i).Single();
            pr.ProductName = proName;
            pr.Price = price;
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    public List<tblProduct> LoadProduct(string ctid)
    {
        var x = from i in this.dtc.tblProducts
                where i.CatalogID == ctid
                orderby i.ProductID descending
                select i;
        return x.ToList();
    }
    #endregion
    #region Categories
    public List<tblCategory> GetCategory()
    {
        var x = from i in this.dtc.tblCategories
                orderby i.CategoriesID ascending
                select i;
        return x.ToList();
    }
    public string GetCategoryName(string id)
    {
        tblCategory x = (from i in this.dtc.tblCategories
                         where i.CategoriesID == id
                         select i).First();
        return x.CategoriesName;
    }
    public tblCategory GetCategoryByName(string Name)
    {
        tblCategory x = (from i in this.dtc.tblCategories
                         where i.CategoriesName.Contains(Name)
                         select i).First();
        return x;
    }
    public string GetCategoryID()
    {
        tblCategory d = (from t in this.dtc.tblCategories
                        where t.Stt == this.dtc.tblCategories.Max(t1 => t1.Stt)
                        select t).First();
        int id = d.Stt + 1;
        return "CT0" + id;

    }
    public bool DelCategory(string ctid)
    {
        try
        {
            var x = from i in this.dtc.tblCategories
                    where i.CategoriesID.Equals(ctid)
                    select i;
            this.dtc.tblCategories.DeleteOnSubmit(x.First());
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }
    public bool UpdateCategory(string Ctid, string CtName, string ClId)
    {
        try
        {
            tblCategory pr = (from i in this.dtc.tblCategories where i.CategoriesID == Ctid select i).Single();
            pr.CatalogID = ClId;
            pr.CategoriesName = CtName;
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    public bool AddCategory(string Ctid, string CtName, string ClId)
    {
        try
        {
            tblCategory pr = new tblCategory();
            pr.CategoriesID = Ctid;
            pr.CatalogID = ClId;
            pr.CategoriesName = CtName;
            this.dtc.tblCategories.InsertOnSubmit(pr);
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    #endregion
    #region Catalog
    public List<tblCatalog> GetCatalog()
    {
        var x = from i in this.dtc.tblCatalogs
                orderby i.CatalogID ascending
                select i;
        return x.ToList();
    }
    public string GetCatalogName(string id)
    {
        tblCatalog x = (from i in this.dtc.tblCatalogs
                        where i.CatalogID == id
                        select i).First();
        return x.CatalogName;
    }
    public tblCatalog GetCatalogByName(string name)
    {
        tblCatalog x = (from i in this.dtc.tblCatalogs
                        where i.CatalogName.Contains(name)
                        select i).First();
        return x;
    }
    public string GetCatalogID()
    {
        tblCatalog d = (from t in this.dtc.tblCatalogs
                         where t.Stt == this.dtc.tblCatalogs.Max(t1 => t1.Stt)
                         select t).First();
        int id = d.Stt + 1;
        return "CL0" + id;

    }
    public bool DelCatalog(string clid)
    {
        try
        {
            var x = from i in this.dtc.tblCatalogs
                    where i.CatalogID.Equals(clid)
                    select i;
            this.dtc.tblCatalogs.DeleteOnSubmit(x.First());
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }
    public bool UpdateCatalog(string Clid, string ClName)
    {
        try
        {
            tblCatalog pr = (from i in this.dtc.tblCatalogs where i.CatalogID == Clid select i).Single();
            pr.CatalogID = Clid;
            pr.CatalogName = ClName;
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    public bool AddCatalog(string Clid, string ClName)
    {
        try
        {
            tblCatalog cl = new tblCatalog();
            cl.CatalogID = Clid;
            cl.CatalogName = ClName;
            this.dtc.tblCatalogs.InsertOnSubmit(cl);
            this.dtc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;

    }
    #endregion
}